package com.hx.config;

import com.hx.model.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Enumeration;
import java.util.Map;

/**
 * {@link HandshakeInterceptor} 自定义捂手拦截器
 *
 * @author zhangziyao
 * @see #beforeHandshake 在握手前执行
 * @see #afterHandshake 握手后执行
 */
//它与AuthChannelInterceptor方法一个任务，这个类得不到token，固废弃
@Deprecated
@Slf4j
@Component
public class WebSocketHandshakeInterceptor implements HandshakeInterceptor {
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler webSocketHandler, Map<String, Object> map) throws Exception {
        /*if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest serverHttpRequest = (ServletServerHttpRequest) request;
            // TODO: 2022/12/15 握手前置监听 可以验证token是否有效
            String authorization = serverHttpRequest.getServletRequest().getParameter("Authorization");
            Claims allClaimsFromToken = jwtTokenUtils.getAllClaimsFromToken(authorization);
            if (authorization != null
                    && jwtTokenUtils.validateToken(authorization, allClaimsFromToken.getSubject())
                    && !redisUtil.hasKey(RedisConstant.EXPIRATION_TOKEN.concat(CommonConstant.COLON).concat(authorization))) {
                map.put(UserInfo.ID, allClaimsFromToken.get(UserInfo.ID, String.class));
                return true;
            }
        }*/

        //https://zhuanlan.zhihu.com/p/643886168
        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest serverHttpRequest = (ServletServerHttpRequest) request;
            // TODO: 2022/12/15 握手前置监听 可以验证token是否有效
            log.info("1，通过header，键nativeHeaders:{}",serverHttpRequest.getServletRequest().getHeader("nativeHeaders"));
            log.info("2，通过header，键Authorization:{}",serverHttpRequest.getServletRequest().getHeader("Authorization"));
            Enumeration<String> headerNames = serverHttpRequest.getServletRequest().getHeaderNames();
            while (headerNames.hasMoreElements()) {
                log.info("{}",headerNames.nextElement());
            }
            log.info("3，通过参数query:{}",serverHttpRequest.getServletRequest().getParameter("query"));
            /*Claims allClaimsFromToken = jwtTokenUtils.getAllClaimsFromToken(authorization);
            if (authorization != null
                    && jwtTokenUtils.validateToken(authorization, allClaimsFromToken.getSubject())
                    && !redisUtil.hasKey(RedisConstant.EXPIRATION_TOKEN.concat(CommonConstant.COLON).concat(authorization))) {
                map.put(UserInfo.ID, allClaimsFromToken.get(UserInfo.ID, String.class));
                return true;
            }*/
            //通过token获取得到的用户对象
            SysUser user = new SysUser();
            user.setUsername("admin");
            map.put("currentUser",user);
            return true;
        }
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {
        // TODO: 2022/12/15 断开连接监听
        log.info("-----握手之后-----");
    }
}
